const app = getApp()
let qCloud = require("../../index.js");

Page({
    /** 页面的初始数据 */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        produceTaskId: "", // 生产任务ID
        produceTask: {},

        showModalStatus: false,
        taskProcessList: [],
        searchProcessList: [],

        staffList: [],

    },

    /** 生命周期函数--监听页面加载 */
    onLoad: function (options) {
        let self = this;
        self.setData({
            produceTaskId: options.produceTaskId
        });

        if (options.produceTaskId) {
            let params = {
                produceTaskId: self.data.produceTaskId
            }
            app.fetchApis(self, qCloud.api.producetask_queryProduceTaskDetails, params, "POST", function (e) {
                if (e.data.code == 200) {
                    self.setData({
                        produceTask: e.data.data
                    });
                }
            });

            self.queryTaskStaffWorkReportList();
        }
    },

    queryTaskStaffWorkReportList() {
        let self = this;
        let params = {
            produceTaskId: self.data.produceTaskId
        }
        app.fetchApis(self, qCloud.api.workreport_queryTaskStaffWorkReportList, params, "POST", function (e) {
            if (e.data.code == 200) {
                self.setData({
                    taskProcessList: e.data.data.taskProcessList,
                    staffList: e.data.data.staffList
                });
            }
        });
    },

    addProcessTab: function (e) {
        let index = e.currentTarget.dataset.index;

        // 获取操作对象
        let staffList = this.data.staffList;

        staffList[index].staffUpcountList.push({
            approvalQuantity: 0,
            deletedMark: false,
            modifyProcessMark: true,
            modifyMark: true,
            produceTaskId: Number.parseInt(this.data.produceTaskId),
            produceTaskProcessId: null,
            produceTaskProcessName: null,
            unfinishedQuantity: 0,
            settleSalary: "0",
            submitTime: qCloud.util.parseTime(new Date(), '{y}-{m}-{d} {h}:{i}'),
            submitTotal: 0,
            throughQuantity: 0,
            unitPrice: 0.00,
            workerId: null,
            workerName: '',
            workerUpcountId: null
        })

        this.setData({
            staffList: staffList
        })
    },

    delProcessTab: function (e) {
        let index = e.currentTarget.dataset.index;
        let parent = e.currentTarget.dataset.parent;

        // 获取操作对象
        let staffList = this.data.staffList;
        if (staffList[parent].staffUpcountList[index].workerUpcountId == null) {
            staffList[parent].staffUpcountList.splice(index, 1);
        } else {
            staffList[parent].staffUpcountList[index].deletedMark = true;
        }

        this.setData({
            staffList: staffList
        })
    },

    changeProcessName: function (e) {
        let parent = e.currentTarget.dataset.parent;
        let index = e.currentTarget.dataset.index;

        let staffList = this.data.staffList;
        let staffUpcountList = staffList[parent].staffUpcountList;

        let searchProcessList = [];
        this.data.searchProcessList = [];

        for (let i = 0; i < this.data.taskProcessList.length; i++) {
            let process_0 = this.data.taskProcessList[i];
            let flag = false;
            for (let j = 0; j < staffUpcountList.length; j++) {
                let staff_1 = staffUpcountList[j];
                if (process_0.produceTaskProcessId == staff_1.produceTaskProcessId && !staff_1.deletedMark) {
                    flag = true;
                    break;
                }
            }

            if (!flag) {
                searchProcessList.push({
                    parentIndex: parent,
                    staffIndex: index,
                    processSubmitTotal: process_0.processSubmitTotal,
                    unitPrice: process_0.unitPrice,
                    produceTaskProcessId: process_0.produceTaskProcessId,
                    produceTaskProcessName: process_0.produceTaskProcessName
                })
            }
        }

        this.setData({
            searchProcessList: searchProcessList,
            showModalStatus: true
        })

    },

    submitTotalInput: function (e) {
        let index = e.currentTarget.dataset.index;
        let parent = e.currentTarget.dataset.parent;
        let inputValue = e.detail.value == "" ? 0 : Number.parseInt(e.detail.value);

        // 员工列表
        let staffList = this.data.staffList;

        let submitTotal = Number.parseInt(staffList[parent].staffUpcountList[index].submitTotal);
        let unfinishedQuantity = Number.parseInt(staffList[parent].staffUpcountList[index].unfinishedQuantity);

        let newUnfinishedQuantity = unfinishedQuantity + submitTotal - inputValue;
        staffList[parent].staffUpcountList[index].submitTotal = inputValue;

        let produceTaskProcessId = staffList[parent].staffUpcountList[index].produceTaskProcessId;
        for (let i = 0; i < staffList.length; i++) {
            if (staffList[i].staffUpcountList.length > 0) {
                for (let j = 0; j < staffList[i].staffUpcountList.length; j++) {
                    if (staffList[i].staffUpcountList[j].produceTaskProcessId === produceTaskProcessId) {
                        staffList[i].staffUpcountList[j].unfinishedQuantity = newUnfinishedQuantity
                    }
                }
            }
        }
        let taskProcessList = this.data.taskProcessList;
        for (let i = 0; i < taskProcessList.length; i++) {
            if (taskProcessList[i].produceTaskProcessId === produceTaskProcessId) {
                taskProcessList[i].processSubmitTotal = newUnfinishedQuantity
            }
        }


        this.setData({
            staffList: staffList,
            taskProcessList: taskProcessList
        })
    },

    hideStaffModal: function (e) {
        let animation = wx.createAnimation({
            duration: 200,
            timingFunction: "linear",
            delay: 0
        });
        this.animation = animation;
        animation.translateY(300).step();
        this.setData({
            animationData: animation.export(),
        });
        setTimeout(function () {
            animation.translateY(0).step();
            this.setData({
                animationData: animation.export(),
                searchProcessList: [],
                showModalStatus: false
            })
        }.bind(this), 200);
    },

    searchProcessInput: function (e) {
        let self = this;
        let taskProcessList = self.data.taskProcessList;
        if (e.detail.value === '') {
            self.setData({
                searchProcessList: []
            })
        } else {
            let searchProcessList = [];
            for (let i = 0; i < taskProcessList.length; i++) {
                let process = taskProcessList[i];
                if (process.produceTaskProcessName.indexOf(e.detail.value) != -1) {
                    searchProcessList.push({
                        parentIndex: null,
                        staffIndex: null,
                        processSubmitTotal: 0,
                        unitPrice: 0,
                        produceTaskProcessId: process.produceTaskProcessId,
                        produceTaskProcessName: process.produceTaskProcessName
                    });
                }
            }

            self.setData({
                searchProcessList: searchProcessList
            })
        }
    },

    // 确认选择
    confirmProcess: function (e) {
        let index = e.currentTarget.dataset.index;
        let parent = e.currentTarget.dataset.parent;
        let staff = e.currentTarget.dataset.staff;

        let staffList = this.data.staffList;
        staffList[parent].staffUpcountList[staff].produceTaskProcessId = this.data.searchProcessList[index].produceTaskProcessId;
        staffList[parent].staffUpcountList[staff].produceTaskProcessName = this.data.searchProcessList[index].produceTaskProcessName;
        staffList[parent].staffUpcountList[staff].unitPrice = this.data.searchProcessList[index].unitPrice;
        staffList[parent].staffUpcountList[staff].unfinishedQuantity = this.data.searchProcessList[index].processSubmitTotal;

        this.setData({
            staffList: staffList,
            showModalStatus: false
        })
    },

    submitProcessUpcountQuantity: function () {
        let self = this;
        let params = {
            produceTaskId: Number.parseInt(this.data.produceTaskId),
            staffList: self.data.staffList
        }

        app.fetchApis(self, qCloud.api.workreport_submitTaskStaffWorkReport, params, "POST", function (e) {
            if (e.data.code == 200) {
                wx.showToast({
                    title: "上数成功",
                    icon: "success"
                })
                self.queryTaskStaffWorkReportList();
            } else {
                wx.showToast({
                    title: e.data.msg,
                    icon: 'error'
                })
            }
        });
    },
});
